package com.hspedu.homework;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/1/29
 * @time 19:06
 */
public class Homework06 {
    public static void main(String[] args) {
        Person pe = new Person("唐僧", new Horse());
        pe.common();
        pe.passRiver();
        pe.common();
        pe.passMountain();
        }
}

interface Vehicles{
    void work();
}

class Horse implements Vehicles{
    @Override
    public void work() {
        System.out.println("获得了一匹马");
    }
}

class Boat  implements Vehicles{
    @Override
    public void work() {
        System.out.println("获得了一艘船");
    }
}

class Plane implements Vehicles{
    @Override
    public void work() {
        System.out.println("获得了一艘飞机");
    }
}

class Person{
    private String name;
    private Vehicles vehicle;

    public Person(String name, Vehicles vehicle) {
        this.name = name;
        this.vehicle = vehicle;
    }

    public void passRiver(){
        if (!(vehicle instanceof Boat)){
            vehicle = VehicleFactory.getBoat();
        }
        vehicle.work();
    }

    public void common(){
        if (!(vehicle instanceof Horse)){
            vehicle = VehicleFactory.getHorse();
        }
        vehicle.work();
    }

    public void passMountain(){
        if (!(vehicle instanceof Plane)){
            vehicle = VehicleFactory.getPlane();
        }
        vehicle.work();
    }

}
class VehicleFactory{
    private static Vehicles horse = new Horse();
    private static Vehicles plane = new Plane();
    public static Vehicles getHorse(){
        return horse;
    }
    public static Vehicles getPlane(){
        return plane;
    }

    private VehicleFactory(){}

    public static Vehicles getBoat(){
        return new Boat();
    }
}
